<?php
class BookModel
{
    private $conn;

    public function __construct($conn)
    {
        $this->conn = $conn;
    }
    public function getBookById($id)
    {
        // 书籍信息存储在数据库中，可以从数据库中获取指定ID的书籍信息
        //  $book = $this->db->query("SELECT * FROM books WHERE id = ?", [$id])->fetch();

        // 返回获取到的书籍信息
        // return $book;
    }
    // 实现图书的查询功能
    public function getAllBooks()
    {
        $sql = "SELECT * FROM books";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            throw new Exception("Prepare failed: " . $this->conn->error);
        }
        $stmt->execute();
        $result = $stmt->get_result();
        $books = [];
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $books[] = $row;
            }
        }
        $stmt->close();
        return $books;
    }

    // 实现图书的新增功能
    public function addBook($title, $author, $isbn, $publishedDate)
    {
        $sql = "INSERT INTO books (title, author, isbn, publishedDate) VALUES (?, ?, ?, ?)";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            throw new Exception("Prepare failed: " . $this->conn->error);
        }
        $stmt->bind_param("ssss", $title, $author, $isbn, $publishedDate);
        $stmt->execute();
        $stmt->close();
    }

    // 实现图书的修改功能
    public function updateBook($id, $title, $author, $isbn, $publishedDate)
    {
        $sql = "UPDATE books SET title = ?, author = ?, isbn = ?, publishedDate = ? WHERE id = ?";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            throw new Exception("Prepare failed: " . $this->conn->error);
        }
        $stmt->bind_param("ssssi", $title, $author, $isbn, $publishedDate, $id);
        $stmt->execute();
        $stmt->close();
    }

    // 实现图书的删除功能
    public function deleteBook($id)
    {
        $sql = "DELETE FROM books WHERE id = ?";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            throw new Exception("Prepare failed: " . $this->conn->error);
        }
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->close();
    }

    // 实现列表分页功能
    public function getBooksWithPagination($limit, $offset)
    {
        $sql = "SELECT * FROM books LIMIT ? OFFSET ?";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            throw new Exception("Prepare failed: " . $this->conn->error);
        }
        $stmt->bind_param("ii", $limit, $offset);
        $stmt->execute();
        $result = $stmt->get_result();
        $books = [];
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $books[] = $row;
            }
        }
        $stmt->close();
        return $books;
    }

    // 获取总图书数量
    public function getTotalBooks()
    {
        $sql = "SELECT COUNT(*) as total FROM books";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            throw new Exception("Prepare failed: " . $this->conn->error);
        }
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        $stmt->close();
        return $row['total'];
    }
}
